package com.hxy.controller;

import com.hxy.common.JsonResult;
import com.hxy.pojo.model.AdminPermissionView;
import com.hxy.service.UserInfService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录控制
 * @author feiyu
 */
@Controller
@RequestMapping("/admin")
public class UserInfController {
    @Autowired
    private UserInfService userInfService;

    /**
     *
     * @param username
     * @param password
     * @param rememberMe
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam(value = "rememberMe", required = false, defaultValue = "false") boolean rememberMe) {
        //创建UsernamePasswordToken对象,2个参数
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //UsernamePasswordToken对象设置记住我 :rememberMe
        token.setRememberMe(rememberMe);
        //获取用户eid
        Integer eid=userInfService.findEidByUsername(username);
        try {
            System.out.println(username+password);
            //登录
            SecurityUtils.getSubject().login(token);
            //通过捕获异常判断用户名或者密码错误
            return new JsonResult(0, "登录成功", null);
        } catch (UnknownAccountException uae) {
            return new JsonResult(1, "用户名错误", null);
        } catch (IncorrectCredentialsException ice) {
            return new JsonResult(1, "密码错误", null);
        }
    }
    /**
     * 登出注销会删除浏览器cookie中的rememberMe
     * @return
     */
    @RequestMapping("/logout")
    @ResponseBody
    public JsonResult logout() {
        SecurityUtils.getSubject().logout();
        return new JsonResult(0, "登出成功", null);
    }

    /** 修改者 陈雄 --- 将权限集合改成获取单个权限
     * 登录信息权限
     * @return
     */
    @RequestMapping("/info")
    @ResponseBody
    public JsonResult info() {
        //获取用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();

        //获取权限集合  改为获取单个 已添加新的service方法
        String permissionName = userInfService.findPermissionNameByUsername(username);
        //组装返回modelview

        // 由于vue前端内中只读取返回的用户权限对象中的集合权限属性，所以一个权限也需要用list集合封装
        List<String> permissionsName = new ArrayList<>();
        // 将权限放进集合中
        permissionsName.add(permissionName);
        AdminPermissionView adminPermissionView = new AdminPermissionView(username, permissionsName);
        return new JsonResult(0, "", adminPermissionView);
    }

}
